package com.wy.config;

import com.wy.controller.BaseInfoController;
import com.google.common.collect.Lists;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.security.Timestamp;
import java.util.Date;
import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {

        ParameterBuilder authBuilder = new ParameterBuilder();
        authBuilder.name("authentication")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false).build();
        List<Parameter> list = Lists.newArrayList();
        list.add(authBuilder.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(list)
                .apiInfo(apiInfo())
                .directModelSubstitute(Timestamp.class, Long.class)//将Timestamp类型全部转为Long类型
                .directModelSubstitute(Date.class, Long.class)//将Date类型全部转为Long类型
                .select()
                //选择controller包
                .apis(RequestHandlerSelectors.basePackage(BaseInfoController.class.getPackage().getName()))// 扫描接口的包路径，
                .paths(PathSelectors.any())

                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //自定义信息可按需求填写
                .description("Employee Management REST API")
                .contact(new Contact("Ramesh Fadatare", "www.javaguides.net", "ramesh24fadatare@gmail.com"))
                .license("Apache 2.0")
                .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
                .version("1.0.0")
                .build();
    }
}
